package main

import (
    "fmt"
    "encoding/json"
)

type Movie struct{
    Title string    `json:"title"`
    Year int        `json:"year"`
    Price int       `json:"rmb"`
    Actors []string `json:"actors"`
}

func main(){
    movie := Movie{"喜剧之王",1998,2,[]string{"周星驰","张柏芝"}}
    //编码 结构体 =====> json
    jsonStr,err := json.Marshal(movie)
    if err != nil {
        fmt.Println("json marshal error",err)
        return
    }
    fmt.Printf("jsonStr = %s\n",jsonStr)

    //解码 json ======> 结构体
    //jsonStr = {"title":"喜剧之王","year":1998,"rmb":2,"actors":["周星驰","张柏芝"]}
    myMovie := Movie{}
    err = json.Unmarshal(jsonStr,&myMovie)
    if err != nil {
        fmt.Println("json unmarshal error",err)
        return
    }

    fmt.Printf("%v\n",myMovie)
}